using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using T2.Logging.ASPMVC.Entity.Interface;
using T2.Logging.ASPMVC.Entity;
using T2.Logging.Business.Entities;
using T2.Logging.Business.Entities.Interfaces;
using T2.Logging.ASPMVC.Entity.Search;
using T2.Logging.ASPMVC.Entity.Search.Interface;
using T2.Logging.Business.Entities.Search;
using T2.Logging.Business.Entities.Search.Interfaces;
using T2.Logging.ASPMVC.Entity.Translators;
using T2.Logging.ASPMVC.Entity.Translators.Search;
using System.Web.Security;
using System.Activities;
using T2.Logging.Business.Workflows;


namespace T2.Logging.ASPMVC.ViewModel
{

	public class ApplicationViewModel
	{
		Business.BusinessService bs = new Business.BusinessService();
		public ApplicationViewModel(bool loadParent)
		{
		if (loadParent)
			{
				this.LoadTenantList();
			}
		}
		
		private String m_header;
		public String Header
        {
            get
            {
                return "Application";
            }
			
			set
            {
                m_header = value;
            }
        }
		
		private String m_title;
		public String Title
        {
            get
            {
                return "Application";
            }
			
			set
            {
                m_title = value;
            }
        }
		
		private IApplication_ui m_model;
		public IApplication_ui Model
		{
			get
			{
				return m_model;
			}
			set
			{
				m_model = value;
			}
		}
		private List<ITenant_ui> m_tenantlist;
		public List<ITenant_ui> Tenant_List
		{
			get
			{
				return m_tenantlist;
			}
			set
			{
				m_tenantlist = value;
			}
		}

		 private void LoadTenantList()
		{
			ITenant_biz param = new Tenant_biz();
			param.Action = "GetAll";
			param.PageNumber = 1;
			param.PageSize = 20;
			param.SortColumn = "TenantID";
			param.SortType = "desc";
			param.TotalRows = -1;
			List<ITenant_biz> retVal = bs.TenantController_Get(param, true, true);
			List<ITenant_ui> LoadRetVal = new List<ITenant_ui>();
			TenantUITranslator trans = new TenantUITranslator();
			foreach (ITenant_biz item in retVal)
			{
				ITenant_ui temp = new Tenant_ui();
				temp = trans.TranslateFromBusinessLayer(item);
				LoadRetVal.Add(temp);
			}
			m_tenantlist = LoadRetVal;
		}


        
		
		public void LoadModel(IApplication_ui param)
        {
            ApplicationUITranslator trans = new ApplicationUITranslator();
            IApplication_biz bizParam = trans.TranslateToBusinessLayer(param);
            List<IApplication_biz> tempRetVal = bs.ApplicationController_Get(bizParam, true, true);
            IApplication_ui retVal = new Application_ui();
            if (tempRetVal != null && tempRetVal.Count > 0)
            {
                retVal = trans.TranslateFromBusinessLayer(tempRetVal.First());
            }

            m_model = retVal;

        }

        public Int32 Set(IApplication_ui param)
        {
            ApplicationUITranslator trans = new ApplicationUITranslator();
            IApplication_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();

            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            //wfInput.Add("UserName", Membership.GetUser().UserName);

            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationNonQuery(), wfInput);

            IDictionary<string, int> r = (IDictionary<string, int>)wfOutput["RetVal"];

            //IDictionary<string,int> r = bs.ApplicationController_Set(bizParam);
            var ret = r.ToList().Select(s => new { Key = s.Key, Value = s.Value });
            Int32 retVal = r.Where(z => z.Key == "Return_Value").Select(p => p.Value).FirstOrDefault(); 
			return retVal;
        }

       

        public List<IApplication_ui> SearchList(IApplicationSearch_ui param)
        {
            ApplicationUISearchTranslator transSearch = new ApplicationUISearchTranslator();
            IApplicationSearch_biz bizParam = transSearch.TranslateToBusinessLayer(param);
            List<IApplication_biz> retValBiz = bs.ApplicationController_Search(bizParam, true, true);
            List<IApplication_ui> retVal = new List<IApplication_ui>();
            ApplicationUITranslator trans = new ApplicationUITranslator();
            foreach (IApplication_biz item in retValBiz)
            {
                IApplication_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }

        

        public List<IApplication_ui> ModelList(IApplication_ui param)
        {

            ApplicationUITranslator trans = new ApplicationUITranslator();
            IApplication_biz bizParam = trans.TranslateToBusinessLayer(param);

            IDictionary<string, object> wfInput = new Dictionary<string, object>();

            wfInput.Add("BusinessService", bs);
            wfInput.Add("Param", bizParam);
            wfInput.Add("ParamSearch", null);
            wfInput.Add("RoleNames", Roles.GetRolesForUser());
            
            // activation of workflow
            IDictionary<string, object> wfOutput = WorkflowInvoker.Invoke(new ApplicationQuery(), wfInput);

            IApplication_biz[] retValBiz = (IApplication_biz[])wfOutput["RetValCollection"];

            //List<IApplication_biz> retValBiz = bs.ApplicationController_Get(bizParam, true, true);
            List<IApplication_ui> retVal = new List<IApplication_ui>();
            foreach (IApplication_biz item in retValBiz)
            {
                IApplication_ui temp = trans.TranslateFromBusinessLayer(item);
                retVal.Add(temp);
            }

            
            return retVal;

        }
		
	}
}
